<@override name="contest_content">
<div class="pagination pagination-centered">
  <div class="form-inline">
    <form class="form-search" action="contest/status/${cid!}">
      <input type="text" class="input-small" name="name" value="${name!}" placeholder="User Name">
      <select class="input-medium" id="inputProblem" name="pid">
        <option value="">Problem</option>
        <#if contestProblems??>
          <#list contestProblems as Problem>
            <option value="${Problem.id!}"<#if pid?? && pid==Problem.id>selected</#if>>${Problem.id!}
              . ${Problem.title!}</option>
          </#list>
        </#if>
      </select>
      <select class="input-medium" id="inputLanguage" name="language">
        <option value="0">Language</option>
        <#if program_languages??>
          <#list program_languages.keySet() as key>
            <option value="${key!}"
                    <#if language?? && language==key>selected</#if>>${program_languages.get(key)!}</option>
          </#list>
        </#if>
      </select>
      <select class="input-medium" id="inputResult" name="result">
        <option value="-1">Result</option>
        <#if judge_result??>
          <#list judge_result as Result>
            <#if Result.id<=16>
              <option value="${Result.id!}"
                      <#if result?? && result?eval==Result.id>selected</#if>>${Result.name!}</option>
            </#if>
          </#list>
          <option value="999" <#if result?? && result?eval==999>selected</#if>>Not-AC</option>
        </#if>
      </select>
      <button type="submit" class="btn btn-info">Go</button>
    </form>
  </div>

  <#include "../common/_paginate2.html" />
  <@paginate2 currentPage=solutionList.pageNumber totalPage=solutionList.totalPage actionUrl="contest/status/${cid!}" urlParas="${query}" />
  <div class="pull-right">
    <span class="badge badge-info">${solutionList.pageNumber}/${solutionList.totalPage} Pages</span> <span
          class="badge badge-info">${solutionList.totalRow}
    Records</span>
  </div>
</div>

<table id="status" class="table table-stats table-hover table-condensed">
  <thead>
  <tr>
    <th>Run ID</th>
    <th>User</th>
    <th>Problem</th>
    <th>Result</th>
    <th>Time</th>
    <th>Memory</th>
    <th>Language</th>
    <th>Code Length</th>
    <th>Submit Time</th>
  </tr>
  </thead>
  <tbody>
    <#if solutionList??>
      <#list solutionList.list as solution>
      <tr>
        <td class="sid">${solution.sid!}</td>
        <td class="user" uid="${solution.uid}">
          <a href="user/profile/${(solution.name)!}" data-toggle="tooltip" target="_blank"
             title="${(solution.nick)!?html}">${(solution.name)!}</a>
        </td>
        <td class="problem"><a href="contest/problem/${cid}-${solution.alpha!}">${solution.alpha!}</a></td>
        <td class="result" id="${solution.resultName!}">
          <i class="btn" sid="${solution.sid!}">
              ${solution.resultLongName!}
            <#if solution.test?? && solution.test !=0>
               on test ${solution.test!}
            </#if>
          </i>
        </td>
        <td class="time"><#if solution.result==0>${solution.time!0} MS</#if></td>
        <td class="memory"><#if solution.result==0>${solution.memory!0} KB</#if></td>
        <td class="language">
          <#if user?? &&(adminUser?? || user.uid==solution.uid)>
            <a href="contest/code/${cid}-${solution.sid!}" target="_blank"
               title="show source code">${solution.languageName!}</a>
          <#else>
          ${solution.languageName!}
          </#if>
        </td>
        <td class="code_len">${solution.codeLen!} B</td>
        <td class="ctime">${solution.ctime_t!}</td>
      </tr>
      </#list>
    </#if>
  </tbody>
</table>
<div class="pagination pagination-centered">
</div>
<div class="hide" id="contest_code">
  <a href="contest/code/[cid]-[sid]" target="_blank" title="show source code">[sid]</a>
</div>
<div class="modal hide fade contestModal" id="codeModal" cid="${cid!}">
  <div class="modal-header">
    <button class="close" aria-hidden="true" data-dismiss="modal" type="button">×</button>
    <h3 id="codeModalLabel">
      Source Code <span id="sid"></span>
    </h3>
  </div>
  <div class="modal-body">
    <div id="content">
      <img src="assets/images/ajax-loader.gif"/> Loading...
    </div>
  </div>
</div>
</@override>
<@override name="styles">
<link rel="stylesheet" type="text/css" href="assets/prettify/prettify.css"/>
</@override>
<@override name="scripts">
<script type="text/javascript" src="assets/prettify/prettify.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    var prevpage = "/contest/status/${cid!}-${solutionList.pageNumber-1}<#if solutionList.pageSize!=20>?s=${solutionList.pageSize}</#if>"
    var nextpage = "/contest/status/${cid!}-${solutionList.pageNumber+1}<#if solutionList.pageSize!=20>?s=${solutionList.pageSize}</#if>"

    $(document).keydown(function (event) {
        <#if (solutionList.pageNumber>1)>if (event.keyCode == 37)location = prevpage;</#if>
        <#if solutionList.pageNumber<solutionList.totalPage>if (event.keyCode == 39)location = nextpage;</#if>
    });

    $('.result i').each(function () {
      var resultName = $(this).parent().attr('id');
      if (resultName == 'WAIT' || resultName == 'RUN' || resultName == 'REJUDGE' || resultName == 'COM' || resultName == 'QUE') {
        $(this).append(' <img src="assets/images/' + (resultName == 'RUN' ? '' : 'ajax-') + 'loader.gif" alert="Running" width="18"/>');
      }
      $(this).addClass(getBtnClass(resultName));
    });

    function getBtnClass(resultName) {
      var btnClass = 'btn-warning';
      if (resultName == 'AC')
        btnClass = 'btn-success';
      if (resultName == 'PE')
        btnClass = 'btn-info';
      else if (resultName == 'WA')
        btnClass = 'btn-danger';
      else if (resultName == 'VE' || resultName == 'SE')
        btnClass = 'btn-inverse';
      else if (resultName == 'WAIT' || resultName == 'RUN' || resultName == 'REJUDGE' || resultName == 'COM' || resultName == 'QUE')
        btnClass = '';
      return btnClass;
    }

    <#if user??>
      var userId = ${user.uid};
      var isAdmin = <#if adminUser??>true<#else>false</#if>;
      $('.result').delegate("i", "click", function () {
        var sid = $(this).attr('sid');
        var cid = $('#codeModal').attr('cid');
        var uid = $(this).parent().parent().children('.user').attr('uid');
        if (!isAdmin && uid != userId)
          return false;
        $('#codeModal').modal("show");
        var tmp = $('#contest_code').html().replace(/\[sid\]/g, sid).replace(/\[cid\]/g, cid);
        $('#codeModal #sid').html(tmp);
        $.get("/api/contest/code", {cid: cid, sid: sid},
                function (data) {
                  if (data.success) {
                    var code = "<pre class=\"prettyprint lang-" + data.brush + "\">\n";
                    code += "/*\n";
                    code += " * Problem: " + data.solution.cid + "-" + data.alpha + " (" + data.problemTitle + ")\n";
                    code += " * User: " + (data.solution.name || "Guest") + "\n";
                    code += " * Language: " + data.language + "\n";
                    code += " * Result: " + data.resultLongName + "\n";
                    if (data.resultName == 'AC') {
                      code += " * Time: " + data.solution.time + " MS  Memory: " + data.solution.memory + " KB\n";
                    }
                    code += " */\n";
                    code += data.solution.source;
                    code += '</pre>';
                    $('#codeModal #content').html(code).removeClass().addClass('prettyprint');
                    prettyPrint();
                  }
                  else {
                    $('#codeModal #content').html(data.result).removeClass().addClass("alert alert-error");
                  }
                });
      });
    </#if>

    var refresh = setInterval(function () {
      refreshResult();
    }, 250);


    function refreshResult() {
      $('#WAIT, #RUN, #REJUDGE, #COM, #QUE').each(function () {
        var $this = $(this);
        var id = $this.attr('id');
        $.getJSON('/api/contest/getResult', {'cid':${cid!}, 'sid': $this.children('i').attr('sid')}, function (data) {
          var img = '';
          if (data.result.name == 'WAIT' ||
              data.result.name == 'RUN' ||
              data.result.name == 'REJUDGE' ||
              data.result.name == 'COM' ||
              data.result.name == 'QUE') {
            img = ' <img src="assets/images/' + (data.result.name == 'RUN' ? '' : 'ajax-') + 'loader.gif" alert="Running" width="18"/>';
          }
          if (data.result.name != id || data.result.name == 'RUN') {
            $this.attr('id', data.result.name);
            if(data.test == 0) {
                $this.html('<i class="btn ' + getBtnClass(data.result.name) + '" sid="'
                    + data.sid + '">' + data.result.longName + img + '</i>');
            } else {
                $this.html('<i class="btn ' + getBtnClass(data.result.name) + '" sid="'
                    + data.sid + '">' + data.result.longName + ' on test ' + data.test + img + '</i>');
            }

            if (data.result.name == 'AC') {
              $this.siblings('.time').html(data.time + ' MS');
              $this.siblings('.memory').html(data.memory + ' KB');
            }
          }
        });
      });
    }
  });
</script>
</@override>
<@extends name="_layout.html" />
